package com.ticketbooking.service.impl;

import com.ticketbooking.entity.ManagerEntity;
import com.ticketbooking.service.IManagerDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Types;
import java.util.Map;

@Service
@Transactional
public class ManagerDaoImpl implements IManagerDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(ManagerEntity man) {
        String sql = "insert into manager(manname,manpassword,manpermission) values (?,?,?)";
        Object args[] = {man.getManname(),man.getManpassword(),man.getManpermission()};
        int[] argTypes = {Types.VARCHAR,Types.VARCHAR,Types.VARCHAR};
        jdbcTemplate.update(sql,args,argTypes);
    }

    @Override
    public ManagerEntity getManagerByManname(String manname) {
        String sql = "select * from manager m where m.manname = ?";
        Map<String, Object> map = jdbcTemplate.queryForMap(sql,new Object[]{manname},new int[]{Types.VARCHAR});
        ManagerEntity manager = new ManagerEntity();
        if(map.size()>0){
            manager.setManname((String)map.get("manname"));
            manager.setManpassword((String)map.get("manpassword"));
            manager.setManpermission((String)map.get("manpermission"));
        }
        return manager;
    }

    @Override
    public int changePassword(String newPwd, String managername) {
        String sql = "UPDATE manager SET manpassword = ? WHERE manname = ?";
        Object args[] = {newPwd,managername};
        return jdbcTemplate.update(sql,args);
    }
}
